package com.egao.orderB.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
//import com.egao.orderB.mapper.ExStorageMapper;
import com.egao.orderB.biz.*;
import com.egao.orderB.entity.*;
import com.egao.orderB.service.ExStorageService;
import com.egao.orderB.service.StockService;
import org.springframework.stereotype.Service;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 出库信息服务实现类
 * @author : Doman
 * @date : 2022-01-07 17:09:31
 * @version ：1.0
 */
@Service
public class ExStorageServiceImpl /*extends ServiceImpl<ExStorageMapper, ExStorage> */implements ExStorageService {

	@Autowired
	private ExStorageBiz exStorageBiz;
	@Autowired
	private ExGoodsBiz exGoodsBiz;
	@Autowired
	private StockBiz stockBiz;
	@Autowired
	private PutStorageBiz putStorageBiz;
	@Autowired
	private PutInfoBiz putInfoBiz;
	@Autowired
	private PutGoodsBiz putGoodsBiz;

  @Override
  public JsonResult list(PageResult2<T> page, ExStorage exStorage, User loginUser)  throws Exception{
    return exStorageBiz.list(page,exStorage,loginUser);
  }

  @Override
  public JsonResult list(ExStorage exStorage, User loginUser) {
    return JsonResult.ok().setData(exStorageBiz.list(exStorage,loginUser));
  }

  @Override
  public JsonResult listVolume(ExStorage exStorage, User loginUser) {
    return JsonResult.ok().setData(exStorageBiz.listVolume(exStorage,loginUser));
  }

  @Override
  public JsonResult listMonth(ExStorage exStorage, User loginUser) {
    return JsonResult.ok().setData(exStorageBiz.listMonth(exStorage,loginUser));
  }

  @Override
  public JsonResult getById(ExStorage exStorage) {
    return JsonResult.ok().setData(exStorageBiz.getById(exStorage));
  }

  @Transactional
  @Override
  public JsonResult saveExStorage(String exStorage, String stockList, User loginUser) {
    ExStorage exStorage1 = com.alibaba.fastjson.JSONObject.parseObject(exStorage, ExStorage.class);
    List<Stock> stockList2 =  JSON.parseArray(stockList, Stock.class);
    int id = exStorageBiz.save(exStorage1, loginUser);
    for (Stock stock : stockList2) {
      ExGoods exGoods = new ExGoods();
      exGoods.setExStorageId(id);
      exGoods.setCustomerId(stock.getCustomerId());
      exGoods.setCustomer(stock.getCustomer());
      exGoods.setSku(stock.getSku());
      exGoods.setName(stock.getName());
      exGoods.setNum(stock.getNum());
      exGoods.setPack(stock.getPack());
      exGoods.setWeight(stock.getWeight());
      exGoods.setLength(stock.getLength());
      exGoods.setWidth(stock.getWidth());
      exGoods.setHigh(stock.getHigh());
      exGoodsBiz.save(exGoods, loginUser);
      stock.setNum(0 - stock.getNum());
      stockBiz.updateNum(stock, loginUser);
    }
    return JsonResult.ok("出库成功");
  }

  @Transactional
  @Override
  public JsonResult saveExStorageByPutGoods(String putStorageIds, ExStorage exStorage2, User loginUser) throws Exception{
    String[] putStorageIds2 = putStorageIds.split(",");
    int id = 0;

    int packNum = 0;//包装件数
    float num = 0;//件数
    float weight = 0;//重量
    float volume = 0;//体积
    PutStorage putStorage = null;
    for (String putStorageId : putStorageIds2) {
      putStorage = new PutStorage();
      putStorage.setId(putStorageId);
      try {
        putStorage = putStorageBiz.getById(putStorage);
      } catch (Exception e) {
        throw new Exception("入库单id:"+putStorageId+"不存在");
      }
      packNum += Float.parseFloat(putStorage.getPackNum());//包装件数
      num += putStorage.getNum();//件数
      weight += putStorage.getWeight();//重量
      volume += putStorage.getVolume();//体积
      //      PutInfo putInfo = putInfoBiz.getByPutStorageId(putStorageId);
    }

    putStorage.setPackNum(packNum+"");
    putStorage.setNum(num);
    putStorage.setWeight(weight);
    putStorage.setVolume(volume);
    ExStorage exStorage = new ExStorage(putStorage);
    exStorage.setBoxType(exStorage2.getBoxType());
    exStorage.setBillOfLading(exStorage2.getBillOfLading());
    exStorage.setPortOfDeparture(exStorage2.getPortOfDeparture());
    exStorage.setPortOfDestination(exStorage2.getPortOfDestination());
    exStorage.setPutStorageIds(putStorageIds);
    exStorage.setVoyage(exStorage2.getVoyage());
    exStorage.setVessel(exStorage2.getVessel());
    id = exStorageBiz.save(exStorage, loginUser);

    for (String putStorageId : putStorageIds2) {
      PutGoods putGoods = new PutGoods();
      putGoods.setPutInStorageId(Integer.valueOf(putStorageId));
      List<PutGoods> putGoodsList = putGoodsBiz.list(putGoods, loginUser);
      for (PutGoods putGoods1 : putGoodsList) {
        ExGoods exGoods = new ExGoods();
        exGoods.setExStorageId(id);
        exGoods.setCustomerId(putGoods1.getCustomerId());
        exGoods.setCustomer(putGoods1.getCustomer());
        exGoods.setShippingMark(putGoods1.getShippingMark());
        exGoods.setSku(putGoods1.getSku());
        exGoods.setName(putGoods1.getName());
        exGoods.setNum(putGoods1.getNum());
        exGoods.setPack(putGoods1.getPack());
        exGoods.setWeight(putGoods1.getWeight());
        exGoods.setLength(putGoods1.getLength());
        exGoods.setWidth(putGoods1.getWidth());
        exGoods.setHigh(putGoods1.getHigh());
        exGoods.setTotalWeight(putGoods1.getTotalWeight());
        exGoods.setVolume(putGoods1.getVolume());
        exGoods.setTotalVolume(putGoods1.getTotalVolume());
        exGoods.setPalletNum(putGoods1.getPalletNum());
        exGoodsBiz.save(exGoods, loginUser);
        Stock stock = new Stock();
        stock.setCustomerId(putGoods1.getCustomerId());
        stock.setSku(putGoods1.getSku());
        stock.setName(putGoods1.getName());
        stock.setPack(putGoods1.getPack());
        stock.setWeight(putGoods1.getWeight());
        stock.setLength(putGoods1.getLength());
        stock.setWidth(putGoods1.getWidth());
        stock.setHigh(putGoods1.getHigh());
        List<Stock> stocks = stockBiz.listUnique(stock);
        if (stocks == null || stocks.size() == 0) {
          throw new Exception("没有对应出库的商品");
        }
        stock = stocks.get(0);
        stock.setNum(0 - putGoods1.getNum());
        stockBiz.updateNum(stock, loginUser);
      }
    }

    //删除入库单
    for (String putStorageId : putStorageIds2) {
      putStorage = new PutStorage();
      putStorage.setId(putStorageId);
      putStorage.setExStoraged(1);
      putStorageBiz.updateExStoraged(putStorage,new User(1));
    }
    return JsonResult.ok("出库成功");
  }

  @Transactional
  @Override
  public JsonResult save(ExStorage exStorage, User loginUser) {
    exStorageBiz.save(exStorage, loginUser);
    return JsonResult.ok("添加成功");
  }

  @Transactional
  @Override
  public JsonResult update(ExStorage exStorage, User loginUser) {
    if (exStorageBiz.update(exStorage, loginUser)) {
      return JsonResult.ok("修改成功");
    }
    return JsonResult.error("修改失败");
  }

  @Transactional
  @Override
  public JsonResult remove(ExStorage exStorage, User loginUser) {
    String[] strs = exStorage.getId().split(",");
    for (String id : strs) {
      if (id == null || id.equals("")) {
        continue;
      }
      exStorage = new ExStorage();
      exStorage.setId(id);
      if (exStorageBiz.remove(exStorage, loginUser)) {
        ExGoods exGoods = new ExGoods();
        exGoods.setExStorageId(Integer.valueOf(id));
        List<ExGoods> exGoodsList = exGoodsBiz.list(exGoods, loginUser);
        if (exGoodsList != null && exGoodsList.size() > 0) {
          for (ExGoods exGoodsOld : exGoodsList) {
            if (exGoodsBiz.remove(exGoodsOld, loginUser)) {
              ExStorage exStorage2 = new ExStorage();
              exStorage2.setId(exGoodsOld.getExStorageId() + "");
              exStorage2.setNum(0 - exGoodsOld.getNum());
              exStorage2.setTotalGrossWeight((0 - exGoodsOld.getTotalWeight()) + "");
              exStorage2.setVolumeM3((0 - exGoodsOld.getTotalVolume()) + "");
              exStorageBiz.updateNum(exStorage2, loginUser);

              Stock stock = new Stock();
              stock.setCustomerId(exGoodsOld.getCustomerId());
              stock.setSku(exGoodsOld.getSku());
              stock.setName(exGoodsOld.getName());
              stock.setPack(exGoodsOld.getPack());
              stock.setWeight(exGoodsOld.getWeight());
              stock.setLength(exGoodsOld.getLength());
              stock.setWidth(exGoodsOld.getWidth());
              stock.setHigh(exGoodsOld.getHigh());
              List<Stock> stockListUnique = stockBiz.listUnique(stock);
              stock = stockListUnique.get(0);
              stock.setNum(exGoodsOld.getNum());
              stockBiz.updateNum(stock, loginUser);
            }

          }
        }
      }
    }
    return JsonResult.ok("删除成功");
  }

}
